<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1 {
            width: 300px;
            height: 500px;
            /* border: 1px solid gray; */
            font-size: 16px;
            background-color: #ecefff;
            border: 1px solid transparent;
        }

        .title {
            height: 50px;
            margin-top: 20px;
            text-align: center;
            font-size: 16px;
        }

        .box {
            display: flex;
            background-color: white;
            flex-direction: column;
            justify-content: space-between;
            align-items: center;
        }

        .box input {
            width: 200px;
            height: 40px;
            margin-bottom: 20px;
            background-color: rgba(128, 128, 128, 0.296);
            border-radius: 8px;
            border: none;
            font-size: 12px;
        }

        input:active {
            outline: noe;
            /* border: none; */
        }
    </style>
</head>

<body>
    <div class="box1">
        <div class="title">注册新账号</div>
        <div class="box">
            <input type="text" placeholder="请输入账号">
            <input type="text" placeholder="请输入密码">
            <input type="text" placeholder="确认密码">
            <input type="button" value="注册" style="background-color: blue">
        </div>
    </div>

</html>
<script src="./promise-ajax.js"></script>
<script>
    var inputs = document.querySelectorAll('.box1 input')
    inputs[3].onclick = function () {
        if (inputs[0].value && inputs[1].value) {
            if (inputs[1].value == inputs[2].value) {
                if (/^\d{4}$/ .test(inputs[1].value)) {
                    ajax('get', 'https://yantianfeng.com/api/register', { username: inputs[0].value, password: inputs[1].value, }).then( (data)=>{
                        console.log(data);
                        if (data.status == 200) {
                            alert('注册成功')
                        }
                        else {
                            alert('注册失败')
                        }
                    })
                }
                else {
                    alert('密码格式错误')
                }
            }
            else {
                alert('密码不一致')
            }
        }
        else {
            alert('账号密码不能为空')
        }
    }
</script>